/*
 * Copyright (c) 2021. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package cn.edu.ntu.yyl.controller.ucenter;

import cn.edu.ntu.yyl.pojo.article.ArticleCategory;
import cn.edu.ntu.yyl.vo.ArticleVO;
import cn.edu.ntu.yyl.vo.CommentVO;
import cn.edu.ntu.yyl.pojo.user.UserFavorite;
import cn.edu.ntu.yyl.pojo.user.UserInfo;
import cn.edu.ntu.yyl.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/ucenter")
public class UcenterIndexServlet {
    @Autowired
    ArticleService articleService ;
    @Autowired
    CategoryService categoryService ;
    @Autowired
    UserFavoriteService userFavoriteService ;
    @Autowired
    CommentService commentService;
    @Autowired
    ArticleShowService articleShowService;
    @Autowired
    CommentVOService commentShowService;
    //登录
    @RequestMapping({"/login", "/login.html"})
    public String userLogin(){
        return "ucenter/user_login";
    }
    //基本信息
    @RequestMapping({"/ucenter", "/ucenter.html", "/index.html", "/index", "/user"})
    public String ucenter(HttpSession session, Model model){
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        return "ucenter/user";
    }
    //文章管理
    @RequestMapping({"/article", "/article.html"})
    public String article(HttpSession session, Model model){
        UserInfo userInfo = (UserInfo)session.getAttribute("userInfo");
        //按照用户查询文章并且写入model
        List<ArticleVO> articleVOS = articleShowService.Show(articleService.SelectArticleByUserId(userInfo.getId()));
        model.addAttribute("ucenterArticleShows", articleVOS);
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        System.err.println(articleVOS);
        return "ucenter/article/index";
    }
    //文章撰写
    @RequestMapping({"/article/write", "/article/write.html"})
    public String write(HttpSession session, Model model){
        //查询出所有分类和标签并且写入model
        List<ArticleCategory> categories = categoryService.SelectAllArticleCategory();
        List<ArticleCategory> tags = categoryService.SelectTag();
        model.addAttribute("tags",tags);
        model.addAttribute("categories",categories);
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        return "ucenter/article/write";
    }
    //收藏
    @RequestMapping({"/favorite", "/favorite.html"})
    public String favorite(HttpSession session, Model model){
        //按照用户查询出收藏并且写入model
        UserInfo userInfo = (UserInfo)session.getAttribute("userInfo");
        List<UserFavorite> userCollects = userFavoriteService.SelectCollectByUserId(userInfo);
        model.addAttribute("userCollects",userCollects);
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        return "ucenter/article/collect";
    }
    //点赞
    @RequestMapping({"/givelike", "/givelike.html"})
    public String givelike(HttpSession session, Model model){
        //按照用户查询出收藏并且写入model
        UserInfo userInfo = (UserInfo)session.getAttribute("userInfo");
        List<UserFavorite> userFavorites = userFavoriteService.SelectFavoriteByUserId(userInfo);
        model.addAttribute("userFavorites",userFavorites);
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        return "ucenter/article/favorite";
    }
    //评论
    @RequestMapping({"/comments", "/comments.html"})
    public String comments(HttpSession session, Model model){
        UserInfo userInfo = (UserInfo)session.getAttribute("userInfo");
        //按照用户查询文章评论并且写入model
        List<CommentVO> articleComments = commentShowService.Show(commentService.SelectCommentByUser(userInfo));
        model.addAttribute("articleComments",articleComments);
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        return "ucenter/comment";
    }
    //用户全部的信息
    @RequestMapping({"/info", "/info.html"})
    public String info(HttpSession session, Model model){
        //获取用户所有信息并且写入model
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        return "ucenter/user_info";
    }
    //签名
    @RequestMapping({"/signature", "/signature.html"})
    public String signature(HttpSession session, Model model){
        //获取用户所有信息并且写入model
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        return "ucenter/signature";
    }
    //头像设置
    @RequestMapping({"/avatar", "/avatar.html"})
    public String avatar(HttpSession session, Model model){
        //获取用户所有信息并且写入model
        UserInfo userInfo = (UserInfo)session.getAttribute("userInfo");
        String AvatarURL = userInfo.getAvatar();
        //如果没有头像或者头像路径为空,则设置为null
        if (AvatarURL==""||AvatarURL==null){
            AvatarURL = null;
        }
        userInfo.setAvatar(AvatarURL);
        model.addAttribute("userInfo",userInfo);
        session.setAttribute("userInfo",userInfo);
        return "ucenter/avatar";
    }
    //账号密码
    @RequestMapping({"/pwd", "/pwd.html"})
    public String pwd(HttpSession session, Model model){
        //获取用户所有信息并且写入model
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        return "ucenter/pwd";
    }
}
